
#include "led.h"

struct led_obj_t red_led = {
    .light_time = ON_TIME,
    .off_time = OFF_TIME,
    ._state = LED_OFF,
    .state = LED_OFF,
    .led_cb = led_controller_cb,
};

void led_set_state(unsigned char state)
{
    HAL_GPIO_WritePin(
        GPIOA, 
        GPIO_PIN_1, 
        state == 0 ? GPIO_PIN_RESET : GPIO_PIN_SET
    );
}

void led_controller_cb(unsigned char state)
{
    led_set_state(state);
}

void led_blink_controller_handler(struct led_obj_t * led)
{
    if (led->state == LED_ON) {
        led->light_time--;
        if (led->light_time <= 0) {
            led->state = LED_OFF;
            led->off_time = OFF_TIME;
        }
    } else {
        led->off_time--;
        if (led->off_time <= 0) {
            led->state = LED_ON;
            led->light_time = ON_TIME;
        }
    }

    if (led->_state != led->state) {
        led->led_cb(led->state);
        led->_state = led->state;
    }
}
